package fxmedia;

// Import Java JDK Classes
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;

// Import Apache Commons I/O Classes
import org.apache.commons.io.FilenameUtils;

/**
 * @author dca
 */
public class Library extends TreeSet<MediaFile> {
	/** Unique identifier for serialization.<br/> */
	private static final long serialVersionUID = -3066077045176831293L;

	/** Comparator for sorting media files by name.<br/> */
	private static final Comparator<MediaFile> COMPARATOR = new Comparator<MediaFile>() {
		@Override
		public int compare(MediaFile left, MediaFile right) {
			return left.getName().compareTo(right.getName());
		} // END compare
	}; // END comparator

	/**
	 * Constructs the library based on the user&apos;s home music directory.<br/>
	 */
	public Library() {
		this(Constants.DEFAULT_MUSIC_LIBRARY_LOCATION);
	} // END constructor

	/**
	 * Constructs the library based on the supplied directory.<br/>
	 * @param directory The directory for the library.<br/>
	 */
	public Library(String directory) {
		super(COMPARATOR);
		init(new File(directory));
	} // END constructor

	/**
	 * Constructs the library based on the supplied directory.<br/>
	 * @param directory The directory for the library.<br/>
	 */
	public Library(File directory) {
		super(COMPARATOR);
		init(directory);
	} // END constructor

	/**
	 * Builds the tree map, recursively traversing the directory.<br/>
	 * @param directory The directory to traverse.<br/>
	 */
	private void init(File directory) {
		if(null != directory) {
			for(File file : directory.listFiles()) {
				if(file.isDirectory()) {
					init(file);
				} else if(Arrays.asList("mp3", "MP3").contains(FilenameUtils.getExtension(file.getName()))) {
					add(new MediaFile(file.getAbsolutePath()));
				} else if(Arrays.asList("m4a", "M4A").contains(FilenameUtils.getExtension(file.getName()))) {
					System.out.printf("M4A song \"%s\" needs to be converted to MP3%n", file.getAbsolutePath());
				} // END if/else
			} // END loop
		} // END if
	} // END init

	/**
	 * Returns the length of the longest song name.<br/>
	 * @return int The length of the longest song name.<br/>
	 */
	public int getLongestSongName() {
		int length = 0;

		for(MediaFile file : this) {
			if(file.getName().length() > length) {
				length = file.getName().length();
			} // END if
		} // END loop

		return length;
	} // END getLongestSongName

	/**
	 * Returns a long list of song file names.<br/>
	 * @return String the list of song file names.<br/>
	 */
	@Override
	public String toString() {
		final StringBuffer buffer = new StringBuffer(this.size() * 128);

		for(MediaFile file : this) {
			if(!buffer.toString().isEmpty()) {
				buffer.append(System.getProperty("line.separator"));
			} // END if
			buffer.append(file.getName());
		} // END loop

		return buffer.toString();
	} // ENd toString
} // END class Library
